package com.tianma.web.service.master.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.tianma.common.core.domain.entity.SysUser;
import com.tianma.common.utils.StringUtils;
import com.tianma.common.utils.DateUtils;
import com.tianma.system.mapper.SysUserMapper;
import com.tianma.web.domain.master.file.WeekEventDetail;
import com.tianma.web.domain.vo.WeekEventVo;
import com.tianma.web.mapper.master.WeekEventDetailMapper;
import com.tianma.web.service.master.IWeekEventDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.tianma.common.utils.poi.ExcelUtil;
import static com.tianma.common.utils.poi.ExcelUtil.getCellValue;
import com.tianma.common.utils.SecurityUtils;
import com.tianma.web.mapper.master.WeekEventMapper;
import com.tianma.web.domain.master.file.WeekEvent;
import com.tianma.web.service.master.IWeekEventService;


/**
 * 事件记录看版Service业务层处理
 *
 * @author wwf
 * @date 2025-05-20
 */
@Service
public class WeekEventServiceImpl implements IWeekEventService
{
    public static Logger logger= LoggerFactory.getLogger(WeekEventServiceImpl.class);

    @Autowired
    private WeekEventMapper weekEventMapper;
    @Autowired
    private WeekEventDetailMapper weekEventDetailMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private IWeekEventDetailService weekEventDetailService;

    /**
     * 查询事件记录看版
     *
     * @param id 事件记录看版主键
     * @return 事件记录看版
     */
    @Override
    public WeekEvent selectWeekEventById(Integer id)
    {
        return weekEventMapper.selectWeekEventById(id);
    }

    /**
     * 查询事件记录看版列表
     *
     * @param weekEvent 事件记录看版
     * @return 事件记录看版
     */
    @Override
    public List<WeekEvent> selectWeekEventList(WeekEvent weekEvent)
    {
        return weekEventMapper.selectWeekEventList(weekEvent);
    }

    /**
     * 模糊查询事件记录看版列表
     *
     * @param weekEvent 事件记录看版
     * @return 事件记录看版
     */
    @Override
    public List<WeekEvent> selectWeekEventListByLike(WeekEventVo weekEvent)
    {
        return weekEventMapper.selectWeekEventListByLike(weekEvent);
    }

    /**
     * 模糊查询事件记录看版单例
     *
     * @param weekEvent 事件记录看版
     * @return 事件记录看版
     */
    @Override
    public WeekEvent selectWeekEventByOne(WeekEvent weekEvent)
    {
        return weekEventMapper.selectWeekEventByOne(weekEvent);
    }

    /**
     * 新增事件记录看版
     *
     * @param weekEvent 事件记录看版
     * @return 结果
     */
    @Override
    public int insertWeekEvent(WeekEvent weekEvent)
    {
        weekEvent.setCreateUser(SecurityUtils.getUsername());
        weekEvent.setCreateTime(DateUtils.getNowDate());
        weekEvent.setUpdateUser(SecurityUtils.getUsername());
        weekEvent.setUpdateTime(DateUtils.getNowDate());
        return weekEventMapper.insertWeekEvent(weekEvent);
    }

    /**
     * 修改事件记录看版
     *
     * @param weekEvent 事件记录看版
     * @return 结果
     */
    @Override
    public int updateWeekEvent(WeekEvent weekEvent)
    {
        weekEvent.setUpdateTime(DateUtils.getNowDate());
        weekEvent.setUpdateUser(SecurityUtils.getUsername());
        return weekEventMapper.updateWeekEvent(weekEvent);
    }

    /**
     * 批量删除事件记录看版
     *
     * @param ids 需要删除的事件记录看版主键
     * @return 结果
     */
    @Override
    public int deleteWeekEventByIds(Integer[] ids)
    {
        return weekEventMapper.deleteWeekEventByIds(ids);
    }

    /**
     * 删除事件记录看版信息
     *
     * @param id 事件记录看版主键
     * @return 结果
     */
    @Override
    public int deleteWeekEventById(Integer id)
    {
        return weekEventMapper.deleteWeekEventById(id);
    }

    /**
     * 删除全部事件记录看版信息
     *
     * @return 结果
     */
    @Override
    public int removeAll()
    {
        return weekEventMapper.removeAll();
    }


    /**
     * 上传事件记录看版Excel
     *
     */
    @Override
    public void uploadWeekEventExcel(Workbook workbook)
    {
        //TODO 自己实现业务功能
        Sheet sheet = workbook.getSheetAt(0);
        //获取Excel标题名对应导入字段
        Map<String, String> objectFieldNameMap = ExcelUtil.getObjectFieldNameMap(WeekEvent.class);
        //获取标题行索引
        Row titleRow = sheet.getRow(0);
        Map<String, Integer> titleIndexMap = ExcelUtil.getTitleIndexMap(titleRow);
        //设置对象值值
        int lastRowNum = sheet.getLastRowNum();
        for(int rowIndex=1;rowIndex<=lastRowNum;rowIndex++){
            Row row = sheet.getRow(rowIndex);
            String  str = getCellValue(row, 0).toString();
            if(StringUtils.isEmpty(str)){
                continue;
            }
            WeekEvent weekEvent=new WeekEvent();
            insertWeekEvent(weekEvent);
        }
    }

    @Override
    public void publicEventData(WeekEventVo weekEventVo) {
        Integer maninId = weekEventVo.getId();
        String operationType = weekEventVo.getOperationType();//处理类型
        String disposeContext = weekEventVo.getDisposeContext();//处理意见
        String fileId = weekEventVo.getFileId();//文件Id
        String fileName = weekEventVo.getFileName();
        List<String> processUserList = weekEventVo.getProcessUserList();//处理人集合
        String processUser = processUserMarge(processUserList);//处理人

        String eventTitle = weekEventVo.getEventTitle();//事件标题
        String eventContext = weekEventVo.getEventContext();//事件内容
        String eventLevel = weekEventVo.getEventLevel();//事件等级
        Date date = new Date();
        String createuser = SecurityUtils.getNickname();
        WeekEvent weekEvent = new WeekEvent();
        if(maninId==null) {
            //插入主表数据
            weekEvent.setEventTitle(eventTitle);
            weekEvent.setEventContext(eventContext);
            weekEvent.setEventLevel(eventLevel);
            weekEvent.setProcessUser(processUser);//处理人
            weekEvent.setEventStatus(0);//创建
            weekEvent.setCreateTime(date);
            weekEvent.setCreateUser(createuser);
            weekEvent.setUpdateTime(date);
            weekEvent.setUpdateUser(createuser);
            weekEventMapper.insertWeekEvent(weekEvent);

            //生成发起流程数据
            maninId = weekEvent.getId();
            WeekEventDetail mainRecord=new WeekEventDetail();
            mainRecord.setEventId(maninId);
            mainRecord.setUserName(createuser);
            mainRecord.setNextUserName(processUser);//下一节点用户
            mainRecord.setDataType(1);//系统流程
            mainRecord.setOperationType("发起");
            mainRecord.setFileId(fileId);//文件Id
            mainRecord.setFileName(fileName);
            mainRecord.setCreateUser(createuser);
            mainRecord.setCreateTime(date);
            weekEventDetailMapper.insertWeekEventDetail(mainRecord);
        }else{
            weekEvent = weekEventMapper.selectWeekEventById(maninId);
            weekEvent.setEventTitle(eventTitle);
            weekEvent.setEventContext(eventContext);
            weekEvent.setEventLevel(eventLevel);
            weekEvent.setUpdateTime(date);
            weekEvent.setUpdateUser(createuser);
            weekEvent.setProcessUser(processUser);//处理人
            weekEventMapper.updateWeekEvent(weekEvent);
        }
        //查询下一节点是否有记录 没有则插入数据
        WeekEventDetail condition=new WeekEventDetail();
        condition.setEventId(maninId);
        condition.setOperationType(operationType);
        condition.setNextUserName(processUser);
        condition.setUserName(createuser);
        condition.setDataType(1);//系统流程
        WeekEventDetail weekEventDetail = weekEventDetailMapper.selectWeekEventDetailByOne(condition);
        if(weekEventDetail==null){
            condition.setCreateTime(date);
            condition.setCreateUser(createuser);
            condition.setDisposeContext(disposeContext);
            weekEventDetailMapper.insertWeekEventDetail(condition);
        }else{
            weekEventDetail.setDisposeContext(disposeContext);
            weekEventDetailMapper.updateWeekEventDetail(weekEventDetail);
        }
    }

    @Override
    public WeekEventVo selectWeekEventInfo(Integer id) {
        WeekEvent weekEvent = weekEventMapper.selectWeekEventById(id);
        if(weekEvent!=null){
            WeekEventVo weekEventVo = new WeekEventVo();
            BeanUtils.copyProperties(weekEvent, weekEventVo);
            //获取处理内容
            WeekEventDetail lastProcessUser = weekEventDetailMapper.getLastProcessUser(id);
            if(lastProcessUser!=null) {
                String nextUserName = lastProcessUser.getNextUserName();
                weekEventVo.setProcessUser(nextUserName);//处理人
                if(StringUtils.isNotEmpty(nextUserName)) {
                    String nickName = weekEventDetailService.processUserHandler(nextUserName);
                    //查找别名
                    weekEventVo.setNextNickName(nickName);
                }
                weekEventVo.setDisposeContext(lastProcessUser.getDisposeContext());//处理内容
                weekEventVo.setOperationType(lastProcessUser.getOperationType());//处理类型
                weekEventVo.setFileName(lastProcessUser.getFileName());
            }
            return weekEventVo;
        }

        return null;
    }


    /**
     * 合并处理人
     * @param processUserList
     * @return
     */
    public String processUserMarge(List<String> processUserList){
        String processUser="";
        for(String item:processUserList){
            processUser+=item+",";
        }
        if(StringUtils.isNotEmpty(processUser)){
            processUser=processUser.substring(0,processUser.length()-1);
        }
        return processUser;
    }


}
